package pers.xuyong.strive.datastruct.order;

import pers.xuyong.strive.datastruct.order.factory.OrderFactory;

import java.util.Random;

/**
 * 简单排序算法
 *
 * @author billymac
 * @version 1.0
 * @date 17/5/19  下午7:41
 * @since JDK1.8
 */
public class SampleOrder {

    public static Integer[] array = new Integer[100000];


    public static void main(String[] args) {
        initArray();
        long start = System.currentTimeMillis();
        /**
         * 冒泡排序:
         * 数据量(条)          运行时间(ms)
         *  1000                130
         *  10000               2000
         *  100000              131769
         */
//        Order<Integer> order = OrderFactory.getOrder("bubbleOrder");
//        order.sort(array, 0);
//        display(0);
//
//        order.sort(array, 1);
//        display(1);
//
//        order.sort(array, -1);
//        display(-1);
//        System.out.println("runTimes : " + (System.currentTimeMillis() - start));


        /**
         * 选择排序:
         * 数据量(条)          运行时间(ms)
         *  1000                100
         *  10000               925
         *  100000              55000
         */
//        Order<Integer> order = OrderFactory.getOrder("selectorOrder");
//        order.sort(array, 0);
//        display(0);
//
//        order.sort(array, 1);
//        display(1);
//
//        order.sort(array, -1);
//        display(-1);
//        System.out.println("runTimes : " + (System.currentTimeMillis() - start));

        /**
         * 选择排序:
         * 数据量(条)          运行时间(ms)
         *  1000                110
         *  10000               1000
         *  100000              60403
         */
        Order<Integer> order = OrderFactory.getOrder("insertOrder");
        order.sort(array, 0);
        display(0);

        order.sort(array, 1);
        display(1);

        order.sort(array, -1);
        display(-1);
        System.out.println("runTimes : " + (System.currentTimeMillis() - start));
    }


    private static void display(int method) {
        System.out.print("method : " + method + ", ");
        System.out.print("array : ");
        for (int i = 0; i < array.length - 1; i++) {
            System.out.print(array[i] + ", ");
        }
        System.out.println(array[array.length - 1]);

        System.out.println("array.length : " + array.length);
    }

    private static void initArray() {
        Random rand = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = rand.nextInt(10000);
        }
    }
}
